<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\GoodsCategory;

/**
 * 商品分类 DAO
 *
 * @author 谢声强(861167322@qq.com)
 * @package App\Dao
 */
class GoodsCategoryDao extends Dao
{
    use DaoTrait;
    /**
     * 获取商品顶级分类列表
     */
    public function getTopCategory()
    {
        return GoodsCategory::query()->where('pid',0)->select('id', 'pid', 'name', 'icon')->orderByDesc('sort')->get();
    }

    /**
     * 获取商品分类列表
     */
    public function getGoodsCategoryList()
    {
        return GoodsCategory::query()->select('id', 'pid', 'name', 'icon')->orderByDesc('sort')->get();
    }

    /**
     * 通过顶级ID查询次级分类
     * @param int $category_id
     * @return mixed
     */
    public function getSuperCategory(int $category_id)
    {
        return GoodsCategory::query()
            ->where(['pid' => $category_id])
            ->select('id', 'pid', 'name', 'icon')
            ->get();
    }

    /**
     * 获得顶级ID数组查询次级分类
     * @param int $cate_ids
     * @return mixed
     */
    public function getSuperCategoriesByIds(array $cate_ids)
    {
        return GoodsCategory::query()->whereIn('pid', $cate_ids)->select(['id', 'pid', 'name', 'icon'])->get();
    }

    /**
     * 通过商品IDs获取种类
     * @param array $cate_ids
     * @return mixed
     */
    public function getCategoriesByIds(array $cate_ids)
    {
        return GoodsCategory::query()
            ->whereIn('id', $cate_ids)
            ->select('id', 'pid', 'name', 'icon')
            ->get();
    }


    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return GoodsCategory::class;
    }

    /**
     * 获取商品分类通过map
     * @param array $map
     * @param string|null $sort
     * @param array|null $select
     * @param int|null $paginate
     * @return mixed
     */
    public function getGoodsCateByMap(array $map = [], string $sort = null, array $select = null, int $paginate = null)
    {
        $model = self::makeModel();

        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();

    }

    /**
     * 该分类是否存在
     * @param int $id
     * @return bool
     */
    public function checkCateExistById(int $id)
    {
        return GoodsCategory::query()
            ->where('id', $id)
            ->exists();
    }

    public function getCateById(int $id)
    {
        return GoodsCategory::query()->where('id', $id)->get();
    }

}